﻿namespace Utils.DatabaseUtils
{
    using MySql.Data.MySqlClient;
    using System;
    using System.Configuration;
    using System.Data;

    public class MySQLConnect
    {
        public static MySqlConnection GetConnection()
        {
            MySqlConnection CS10000;
            try
            {
                string strConnect = string.Empty;
                if (ConfigurationManager.ConnectionStrings["strConnection_MySQL"] != null)
                {
                    strConnect = ConfigurationManager.ConnectionStrings["strConnection_MySQL"].ConnectionString;
                }
                else
                {
                    strConnect = ConfigurationManager.AppSettings["strConnection_MySQL"];
                }
                MySqlConnection myConnection = new MySqlConnection(strConnect);
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                CS10000 = myConnection;
            }
            catch (MySqlException ex)
            {
                throw ex;
            }
            return CS10000;
        }

        public static MySqlConnection GetConnection(string str_key)
        {
            MySqlConnection CS10000;
            try
            {
                string strConnect = string.Empty;
                if (ConfigurationManager.ConnectionStrings[str_key] != null)
                {
                    strConnect = ConfigurationManager.ConnectionStrings[str_key].ConnectionString;
                }
                else
                {
                    strConnect = ConfigurationManager.AppSettings[str_key];
                }
                MySqlConnection myConnection = new MySqlConnection(strConnect);
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                CS10000 = myConnection;
            }
            catch (MySqlException ex)
            {
                throw ex;
            }
            return CS10000;
        }

        public static string ConnectionString
        {
            get
            {
                if (ConfigurationManager.ConnectionStrings["strConnection_MySQL"] != null)
                {
                    return ConfigurationManager.ConnectionStrings["strConnection_MySQL"].ConnectionString;
                }
                return ConfigurationSettings.AppSettings["strConnection_MySQL"];
            }
        }
    }
}

